package com.jason.quectel.activities;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jason.quectel.R;
import com.jason.quectel.bean.Province;
import com.jason.quectel.interfaces.MyRoomDatabase;
import com.jason.quectel.interfaces.ProvinceDao;

import java.util.List;
import java.util.concurrent.Executors;

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;

public class RoomDbTestActivity extends AppCompatActivity {
    private TextView tvResult;
    private EditText etName;
    private EditText etAge;
    private ProvinceDao provinceDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_test);
        tvResult = findViewById(R.id.tv_result);
        etName = findViewById(R.id.et_name);
        etAge = findViewById(R.id.et_age);
        etAge.setHint("请输入地级市数量");

        String dbName = "quectel";
        MyRoomDatabase database = Room.databaseBuilder(this, MyRoomDatabase.class, dbName).build();
        provinceDao = database.getProvinceDao();
    }

    public void insert(View view) {
        Editable name = etName.getText();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "请先输入名字", Toast.LENGTH_SHORT).show();
            return;
        }
        Editable age = etAge.getText();
        if (TextUtils.isEmpty(age)) {
            Toast.makeText(this, "请先输入地级市数量", Toast.LENGTH_SHORT).show();
            return;
        }
        Province province = new Province();
        province.name = name.toString();
        province.cityCount = Integer.parseInt(age.toString());
        Executors.newCachedThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                provinceDao.insertProvince(province);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvResult.setText("插入成功");
                    }
                });
            }
        });

    }

    public void delete(View view) {
        Editable name = etName.getText();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "请先输入名字", Toast.LENGTH_SHORT).show();
            return;
        }
        Executors.newCachedThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                provinceDao.deleteProvinceByName(name.toString());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvResult.setText("删除成功");
                    }
                });
            }
        });
    }

    public void query(View view) {
        Editable name = etName.getText();
        Executors.newCachedThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                List<Province> list;
                if (TextUtils.isEmpty(name)) {
                    list = provinceDao.queryProvince();
                } else {
                    list = provinceDao.queryProvinceByName(name.toString());
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (list == null) {
                            tvResult.setText("共查询到0条数据");
                        } else {
                            StringBuilder sb = new StringBuilder();
                            for (Province province : list) {
                                sb.append(province.toString()).append("\n");
                            }
                            tvResult.setText("共查询到" + list.size() + "条数据：\n" + sb);
                        }
                    }
                });
            }
        });
    }

    public void update(View view) {
        Editable name = etName.getText();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "请先输入名字", Toast.LENGTH_SHORT).show();
            return;
        }
        Editable age = etAge.getText();
        if (TextUtils.isEmpty(age)) {
            Toast.makeText(this, "请先输入地级市数量", Toast.LENGTH_SHORT).show();
            return;
        }
        Executors.newCachedThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                provinceDao.updateProvinceByName(name.toString(), age.toString());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvResult.setText("更新成功");
                    }
                });
            }
        });
    }

    public void clear(View view) {
        etName.setText("");
        etAge.setText("");
    }
}